#pragma once
#include "BasicRegisterSessionNotification.h"

#include <Wtsapi32.h>
#pragma comment(lib, "Wtsapi32.lib")

#include <tchar.h>

namespace utility
{
	BasicRegisterSessionNotification::BasicRegisterSessionNotification()
		:m_hWnd(NULL)
	{
	}

	BasicRegisterSessionNotification::~BasicRegisterSessionNotification()
	{
	}

	bool BasicRegisterSessionNotification::BasicRegisterSessionNotify(HWND hWnd,bool bAllSession)
	{
		if( !hWnd || !::IsWindow(hWnd) )
			return false;

		BasicUnRegisterSessionNotify();
		
		BOOL bRet = WTSRegisterSessionNotification(hWnd, bAllSession ? NOTIFY_FOR_ALL_SESSIONS : NOTIFY_FOR_THIS_SESSION);
		m_hWnd = hWnd;

		return (TRUE == bRet);
	}

	bool BasicRegisterSessionNotification::BasicUnRegisterSessionNotify()
	{
		BOOL bRet  = TRUE;
		if( m_hWnd )
		{
			bRet = WTSUnRegisterSessionNotification(m_hWnd);
			m_hWnd = NULL;
		}

		return (TRUE == bRet);
	}

	//PROC = typedef int (WINAPI *PROC)();
	typedef BOOL (WINAPI *BasicLockWorkStationProc)();
	bool BasicRegisterSessionNotification::BasicLockWorkStation()
	{
		BOOL bRet = FALSE;
		HMODULE hModule = ::GetModuleHandle(_T("user32.dll"));
		BasicLockWorkStationProc pfnBasicLockWorkStation = (BasicLockWorkStationProc) ::GetProcAddress(hModule, "LockWorkStation");
		if( pfnBasicLockWorkStation )
			bRet = pfnBasicLockWorkStation();
		//LockWorkStation();
		return TRUE == bRet;
	}
}